package com.supertange.miniapp.wepet.modules.wepet.service.impl;

import com.supertange.miniapp.wepet.common.idworker.Sid;
import com.supertange.miniapp.wepet.modules.wepet.entity.*;
import com.supertange.miniapp.wepet.modules.wepet.entity.vo.ExchangeGoodsVO;
import com.supertange.miniapp.wepet.modules.wepet.entity.vo.ExchangeUserVO;
import com.supertange.miniapp.wepet.modules.wepet.entity.vo.ExchangeVO;
import com.supertange.miniapp.wepet.modules.wepet.mapper.ExchangeGoodsMapper;
import com.supertange.miniapp.wepet.modules.wepet.mapper.ExchangeOrderMapper;
import com.supertange.miniapp.wepet.modules.wepet.mapper.ExchangeRecordMapper;
import com.supertange.miniapp.wepet.modules.wepet.mapper.ExchangeUserMapper;
import com.supertange.miniapp.wepet.modules.wepet.service.ExchangeService;
import com.supertange.miniapp.wepet.modules.wepet.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("exchangeService")
public class ExchangeServiceImpl implements ExchangeService {

    @Autowired
    private ExchangeGoodsMapper exchangeGoodsMapper;

    @Autowired
    private ExchangeUserMapper exchangeUserMapper;

    @Autowired
    private ExchangeOrderMapper exchangeOrderMapper;

    @Autowired
    private ExchangeRecordMapper exchangeRecordMapper;

    @Autowired
    private Sid sid;

    @Override
    public List<ExchangeGoodsVO> queryAllGoods(){
        List<ExchangeGoodsEntity> exchangeGoodsEntityList = exchangeGoodsMapper.queryAllGoods();
        List<ExchangeGoodsVO> result = new ArrayList<>();
        for(int i=0;i<exchangeGoodsEntityList.size();i++){
            ExchangeGoodsVO exchangeGoodsVO = new ExchangeGoodsVO();
            ExchangeGoodsEntity exchangeGoodsEntity = exchangeGoodsEntityList.get(i);
            exchangeGoodsVO.setId(exchangeGoodsEntity.getId());
            exchangeGoodsVO.setCost_count(exchangeGoodsEntity.getCost_count());
            exchangeGoodsVO.setImage_url(exchangeGoodsEntity.getImage_url());
            exchangeGoodsVO.setMoney_count(exchangeGoodsEntity.getMoney_count());
            exchangeGoodsVO.setGoods_name(exchangeGoodsEntity.getGoods_name());
            exchangeGoodsVO.setIntroduce(exchangeGoodsEntity.getIntroduce());
            exchangeGoodsVO.setKucun(exchangeGoodsEntity.getKucun());
            exchangeGoodsVO.setColor_list(exchangeGoodsEntity.getColor_list().split(";"));
            exchangeGoodsVO.setEnd_time(exchangeGoodsEntity.getEnd_time());
            result.add(exchangeGoodsVO);
        }
        return result;
    }

    @Override
    public ExchangeVO queryExchangeInfo(String userid) {
        ExchangeUserEntity exchangeUserEntity = exchangeUserMapper.queryUserById(userid);
        if(exchangeUserEntity==null)
            return null;
        List<ExchangeGoodsEntity> exchangeGoodsEntityList = exchangeGoodsMapper.queryAllGoods();
        ExchangeVO exchangeVO = new ExchangeVO();
        exchangeVO.setGoodsInfo(exchangeGoodsEntityList);
        exchangeVO.setUserInfo(exchangeUserEntity);
        return exchangeVO;
    }

    @Override
    public List<ExchangeRecordEntity> queryExchangeRecordByUserid(String userid) {
        return exchangeRecordMapper.queryExchangRecordByUserid(userid);
    }

    @Override
    public void addUser(String userid) {
        ExchangeUserEntity exchangeUserEntity = new ExchangeUserEntity();
        exchangeUserEntity.setId(userid);
        exchangeUserEntity.setFavourate_cnt(0);
        exchangeUserEntity.setYisen_cnt(1000);
        exchangeUserEntity.setFavourate_cnt(50000);
        exchangeUserEntity.setList_addr("");
        exchangeUserEntity.setList_name("");
        exchangeUserEntity.setList_tel("");
        exchangeUserMapper.insert(exchangeUserEntity);
    }

    @Override
    public void addGoods(String image_url,int money_count,int cost_count){
        ExchangeGoodsEntity exchangeGoodsEntity = new ExchangeGoodsEntity();
        exchangeGoodsEntity.setId(sid.nextShort());
        exchangeGoodsEntity.setCost_count(0);
        exchangeGoodsEntity.setMoney_count(money_count);
        exchangeGoodsEntity.setCost_count(cost_count);
        exchangeGoodsEntity.setImage_url(image_url);
        exchangeGoodsMapper.insert(exchangeGoodsEntity);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean addYisen(String userid, int favourate_cnt) {
        if(favourate_cnt<=0)
            return false;
        int yisen_cnt = favourate_cnt/20;
        int res;
        if((res = exchangeUserMapper.addUserYisen(favourate_cnt,yisen_cnt,userid,favourate_cnt))==0)
            return false;
        ExchangeRecordEntity exchangeRecordEntity = new ExchangeRecordEntity();
        exchangeRecordEntity.setId("record"+sid.nextShort());
        exchangeRecordEntity.setDate(new SimpleDateFormat("yyyy年MM月dd日kk点mm分").format(new Date()));
        exchangeRecordEntity.setFavourateCost(favourate_cnt);
        exchangeRecordEntity.setYisenGet(yisen_cnt);
        exchangeRecordEntity.setUserid(userid);
        exchangeRecordMapper.insert(exchangeRecordEntity);
        return true;
    }

    /**
     * -1代表没有找到用户
     * -2代表没有找到商品
     * -3代表用户积分不够
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int addOrder(String kuaidi_address, String kuaidi_name, String kuaidi_tel,
                        String kuaidi_message, String goodsid, String userid, int goods_cnt,
                        String list_addr,String list_name,String list_tel,String list_kucun) {
        ExchangeUserEntity exchangeUserEntity = exchangeUserMapper.queryUserById(userid);
        if(exchangeUserEntity==null){
            return -1;
        }
        ExchangeGoodsEntity exchangeGoodsEntity = exchangeGoodsMapper.queryGoodsById(goodsid);
        if(exchangeGoodsEntity==null){
            return -2;
        }
        if((goods_cnt*exchangeGoodsEntity.getCost_count())>exchangeUserEntity.getYisen_cnt()){
            return -3;
        }
        ExchangeOrderEntity exchangeOrderEntity = new ExchangeOrderEntity();
        exchangeOrderEntity.setId(sid.nextShort());
        exchangeOrderEntity.setGoodscnt(goods_cnt);
        exchangeOrderEntity.setKuaidi_address(kuaidi_address);
        exchangeOrderEntity.setKuaidi_message(kuaidi_message);
        exchangeOrderEntity.setKuaidi_tel(kuaidi_tel);
        exchangeOrderEntity.setKuaidi_name(kuaidi_name);
        exchangeOrderEntity.setUserid(userid);
        exchangeOrderEntity.setGoodsid(goodsid);
        Date now = new Date();
        SimpleDateFormat f= new SimpleDateFormat("yyyy年MM月dd日kk点mm分");
        exchangeOrderEntity.setDate(f.format(now));
        exchangeOrderMapper.insert(exchangeOrderEntity);
        exchangeGoodsMapper.updateGoodsKucun(list_kucun,goodsid);
        exchangeUserMapper.updateUserAfterOrder(goods_cnt*exchangeGoodsEntity.getCost_count(),list_name,list_tel,list_addr,userid);
        return 0;
    }

    @Override
    public boolean addAddr(String listName, String listAddr, String listTel, String userid) {
        int res = exchangeUserMapper.updateUserAddr(listName,listAddr,listTel,userid);
        if(res==1)
            return true;
        else
            return false;
    }


}
